<?php

namespace App\Models;

use App\Enums\GoodsTypeStatusEnum;
use App\Traits\ModelEventTrait;
use Illuminate\Database\Eloquent\SoftDeletes;

class GoodsType extends BaseModel
{
    use ModelEventTrait;
    use SoftDeletes;

    const KEY = 'id';
    const TABLE = 'sp_goods_type';

    protected $table = self::TABLE;
    protected $primaryKey = self::KEY;
    protected $connection = 'mysql';

    // public $timestamps = false;
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
    const DELETED_AT = 'delete_time';

    protected $guarded = [];

    protected $appends = [
        'status_name',
    ];

    protected $hidden = [
        self::DELETED_AT,
    ];

    protected $casts = [
        'create_time' => 'datetime:Y-m-d H:i:s',
        'update_time' => 'datetime:Y-m-d H:i:s',
    ];

    public function getStatusNameAttribute()
    {
        return GoodsTypeStatusEnum::getNameByKey($this->status);
    }

    public function brandInfo()
    {
        return $this->hasOne(BrandModel::class, BrandModel::KEY, 'brand_id');
    }

    public function goodsClassInfo()
    {
        return $this->hasOne(GoodsClass::class, GoodsClass::KEY, 'goods_class_id');
    }
    public function getAll()
    {
        return (new static())->select('id','name','status','goods_class_id','brand_id')->get();
    }
}
